<?php
include_once('config/config.php');
include_once('./config/config.php');
$admin = AuthCheck();
$action = $_GET['action'] ?? '';
// 退出账号
if ($action === 'logout') {
  setcookie('auth', '', time() - 1);
  notice('退出成功', 'login.php');
}
// 接收id
$id = $_GET['id'] ?? 0;
$sql = "SELECT  * FROM `pre_department` WHERE `id` = $id";
$dep = find($sql);
if (!$dep) {
  notice('部门不存在', 'DepList.php');
  exit;
}

// 判断是否有表单提交
if ($_POST) {
  // 接收参数
  $dep = isset($_POST['dep']) ? trim($_POST['dep']) : '';

  // 判断参数是否为空
  if (empty($dep)) {
    notice('部门名称不能为空');
  }
  // 组装数据
  $data = [
    'name' => $dep,
  ];

  $result = update('department', $data, "`id` = '$id'");

  if ($result) {
    notice('修改成功', 'DepList.php');
  } else {
    notice("修改部门失败，请重试", 'DepEdit.php');
  }
}



?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>admin</title>
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">

  <!-- 公共样式 -->
  <?php include_once('./common/meta.php'); ?>
  <style>
    #upload {
      width: 214px;
      height: 200px;
      object-fit: cover;
      cursor: pointer;
    }
  </style>

  <!-- jquery -->
  <script src="./assets/lib/jquery-1.7.2.min.js" type="text/javascript"></script>

</head>

<body>
  <!-- 引入头部 -->
  <?php include_once('common/header.php'); ?>

  <!-- 侧边菜单栏 -->
  <?php include_once('common/menu.php'); ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">添加部门</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="DepList.php">DepList</a> <span class="divider">/</span></li>
      <li class="active">DepAdd</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='DepList.php'"><i class="icon-list"></i> 部门列表</button>
          <div class="btn-group"></div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="post" enctype="multipart/form-data">
                <!-- 用户名 -->
                <label>部门名称</label>
                <input type="text" name="dep" id="dep" placeholder="请输入部门名" class="input-xxlarge" value="<?php echo $dep['name']; ?>">
                <span id="dep-error"></span>

                <label></label>
                <input class="btn btn-primary" type="submit" value="提交" />
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('common/footer.php'); ?>
      </div>
    </div>
  </div>
</body>

</html>

<?php include_once('common/script.php'); ?>

<script>
  $('#dep').change(function() {
    let dep = $.trim($(this).val());

    if (!dep) {
      return false;
    }

    // 发起请求
    $.ajax({
      type: 'post',
      url: 'api.php',
      data: {
        dep,
        action: 'check_dep_name'
      },
      dataType: 'json',
      success: (result) => {
        if (result.code === 1) {
          $('#dep-error').text(result.msg).css({
            color: 'green',
            fontSize: '16px'
          })
        } else {
          $('#dep-error').text(result.msg).css({
            color: 'red',
            fontSize: '16px'
          })
        }
      }
    })
  })
</script>